package com.cetcs.kmga.dataManager.service.impl.feature;

import com.cetcs.kmga.dataManager.dao.mybatis.FeatureCodeMapper;
import com.cetcs.kmga.dataManager.entity.global.AlarmCode;
import com.cetcs.kmga.dataManager.entity.global.CodeType;
import com.cetcs.kmga.dataManager.entity.global.vi.CodeTypeIn;
import com.cetcs.kmga.dataManager.global.DataManagerConstants;
import com.cetcs.kmga.dataManager.service.feature.FeatureCodeService;
import com.cetcs.kmga.dataManager.util.DruidH2Pool;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 特征码实现层
 *
 * @author xutao
 * @version V1.0 创建时间：2017-09-26 10:20
 *          Copyright 2017 by CETC
 */
@Service
public class FeatureCodeSerivceImpl implements FeatureCodeService {

    private static Log LOGGER = LogFactory.getLog(FeatureCodeSerivceImpl.class);

    @Autowired
    private FeatureCodeMapper featureCodeMapper;

    @Override
    public List<AlarmCode> findFeatureCodesByTypeId(Integer typeId) {
        return featureCodeMapper.findFeatureCodesByTypeId(typeId);
    }

    @Override
    public List<AlarmCode> findFeatureCodeByIdOrName(Integer typeId, String code, String name) {
        return featureCodeMapper.findFeatureCodeByIdOrName(typeId, code, name);
    }

    /**
     * 根据数组批量返回特征码
     *
     * @param records 数组名称
     * @return
     */
    @Override
    public Map<Integer, CodeType> findFeatureCodesByTypeIds(List<CodeTypeIn> records) {
        String typeIds = "";
        for (int i = 0; i < records.size(); i++) {
            if (i != 0) {
                typeIds += ",";
            }
            typeIds += records.get(i).getId();
        }
        List<CodeType> dbCodes = featureCodeMapper.findFeatureCodesByTypeIds(typeIds);
        Map<Integer, CodeType> codeTypeMap = new HashMap<>();
        if (dbCodes != null && dbCodes.size() > 0) {
            dbCodes.forEach(p -> {
                List<CodeTypeIn> typeIns = records.stream().filter(k -> k.getId().intValue() == p.getId().intValue()).collect(Collectors.toList());
                if (typeIns != null && typeIns.size() > 0) {
                    p.setType(typeIns.get(0).getType());
                    p.setPage(typeIns.get(0).getPage());
                }
                codeTypeMap.put(p.getId(), p);
            });
        }
        return codeTypeMap;
    }


    /**
     * 根据码表的类型ID与编码值获取ID
     *
     * @param typeId 类型ID
     * @param code   码表值
     * @return
     */
    @Override
    public String acquireValueByCodeAndTypeId(String typeId, String code) {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        String val = "";
        try {
            conn = DruidH2Pool.getInstance().getConnection();
            String querySql = "select value from " + DataManagerConstants.INIT_CD_TYPE_TB + " where typeId = ? and code = ?";
            preparedStatement = conn.prepareStatement(querySql);
            preparedStatement.setString(1, typeId);
            preparedStatement.setString(2, code);
            rs = preparedStatement.executeQuery();
            while (rs.next()) {
                val = rs.getString(1);
            }
        } catch (SQLException e) {
            LOGGER.error("保存数据出错" + e.getMessage(), e);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                DruidH2Pool.getInstance().close(conn);
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        return val;
    }
}
